/**
 * 配置
 * */
package org.zmhhxl.api.auth.domain.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.Hibernate;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;

//配置数据实体，即PO(Persistent Object)
//@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
//@JsonIdentityInfo(
//      generator = ObjectIdGenerators.PropertyGenerator.class,
//      property = "id")
@Entity
@Table(name = "third_party_user")
@Getter
@Setter
@ToString
@EntityListeners(AuditingEntityListener.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true) //相当于Oauth2ThirdAccount
public class ThirdPartyUser implements Serializable {
   private static final long serialVersionUID = -6963523161322981431L;

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "id", nullable = false)
   private Long id;

   /**
    * 三方登录唯一id
    */
   @Column(name = "third_party_id", nullable = false)
   private String thirdPartyId;    // sub, name

   /**
    * 三方登录用户名,临时账号名称（第一次），关联后续使用用户名account_name
    */
   @Column(name = "third_username", nullable = false)
   private String thirdUsername;

   /**
    * 头像地址
    */
   @Column(name = "avatar_url", nullable = true)
   private String avatarUrl;

   /**
    * 三方登录获取的认证信息
    */
   @Column(name = "credentials", nullable = true)
   private String credentials;

   /**
    * 三方登录获取的认证信息的过期时间
    */
   @Column(name = "credentials_expires_at", nullable = true)
   private LocalDateTime credentialsExpiresAt;

   /**
    * 三方登录类型
    */
   @Column(name = "third_party_type", nullable = true)
   private String thirdPartyType;

   @Column(name = "domain", nullable = true)
   private String thirdPartyDomain;  //主域

   @Column(name = "protocol", nullable = true)
   private String thirdPartyProtocol;   //协议

   //配置多表关系,含mappedBy就不应该有@JoinTable或@JoinColumn thirdPartyUsers
   //@JsonBackReference  //排除,避免循环，子对象
   //@JsonManagedReference  //排除,避免循环，父对象
   @ManyToOne(targetEntity = UserInfo.class,
              cascade = CascadeType.ALL,
              fetch = FetchType.LAZY   //mappedBy = "thirdPartyUsers"
   )
   @ToString.Exclude
   @Fetch(FetchMode.JOIN)
   @JsonIgnore  //排除,避免循环    //@JsonProperty("userInfos")
   private UserInfo userInfos = null; //= new HashSet<>();

   @Override
   public boolean equals(Object o) {
      if (this == o) return true;
      if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
      ThirdPartyUser role = (ThirdPartyUser) o;
      return id != null && Objects.equals(id, role.id);
   }

   @Override
   public int hashCode() {
      return getClass().hashCode();
   }

}
